package org.linlinjava.litemall.wx.service;

import java.util.List;
import java.util.Map;

import org.linlinjava.litemall.core.util.ConvertUtils;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.db.service.CustomHerbService;
import org.linlinjava.litemall.db.service.QualificationsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

@Service
public class WxHerbService {
	@Autowired
	private CustomHerbService customHerbService;
	@Autowired
	private QualificationsService qualificationsService;

	/**
	 * 商户认证、经营范围变更药品选择调用
	 * @param userId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @param searchValue
	 * @return
	 */
	public Object selectGenericNameList(Integer userId, Integer page, Integer limit, String sort, String order,
			String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		List<String> querys = Lists.newArrayList();
		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(" (CONCAT(IFNULL(h.generic_name,'')) LIKE '%" + searchValue + "%' ");
			querys.add(" or CONCAT(IFNULL(h.herb_name,'')) LIKE '%" + searchValue + "%' ");
			querys.add(" or CONCAT(IFNULL(h.describe,'')) LIKE '%" + searchValue + "%' )");
		}
		querys.add(" h.deleted = 0 ");
		querys.add(" IFNULL(h.generic_name,'') !='' ");

		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderByClause = sort + " " + order;
		} else {
			orderByClause = " h.generic_name desc ";
		}

		PageHelper.startPage(page, limit);
		List<Map> mapList = customHerbService.getHerbGenerics(query, orderByClause);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

	/**
	 * 个人，用权限查询，通用名和描述接口
	 * @param userId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @param searchValue
	 * @return
	 */
	public Object selectAuthGenericNameList(Integer userId, Integer page, Integer limit, String sort, String order,
			String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		List<String> querys = Lists.newArrayList();
		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(" CONCAT(IFNULL(h.generic_name,''),IFNULL(h.describe,'')) LIKE '%" + searchValue + "%' ");
		}
		querys.add(" u.id = " + userId + " ");
		querys.add(" uha.deleted = 0 ");
		querys.add(" h.deleted = 0 ");
		querys.add(" IFNULL(h.generic_name,'') !='' ");

		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderByClause = sort + " " + order;
		} else {
			orderByClause = " h.generic_name,h.describe desc ";
		}

		PageHelper.startPage(page, limit);
		List<Map> mapList = qualificationsService.getUserHerbGenericDescribeNames(query);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

	/**
	 * 饮片厂，药品厂家、无需权限查询，通用名和描述接口
	 * @param userId
	 * @param page
	 * @param limit
	 * @param sort
	 * @param order
	 * @param searchValue
	 * @return
	 */
	public Object selectGenericNameDescribeList(Integer userId, Integer page, Integer limit, String sort, String order,
			String searchValue) {
		if (userId == null) {
			return ResponseUtil.unlogin();
		}

		List<String> querys = Lists.newArrayList();
		if (!StringUtils.isEmpty(searchValue)) {
			querys.add(" CONCAT(IFNULL(h.generic_name,'')) LIKE '%" + searchValue + "%' ");
		}
		querys.add(" h.deleted = 0 ");
		querys.add(" IFNULL(h.generic_name,'') !='' ");

		String query = StringUtils.collectionToDelimitedString(querys, "and");
		String orderByClause = null;
		if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			orderByClause = sort + " " + order;
		} else {
			orderByClause = " h.generic_name desc ";
		}

		PageHelper.startPage(page, limit);
		List<Map> mapList = customHerbService.getHerbGenericsDescribe(query, orderByClause);
		Page<Map> list = (Page) mapList;

		Map<String, Object> data = Maps.newHashMap();
		data.put("list", ConvertUtils.sourceToCamelCaseMap(list));
		data.put("total", list.getTotal());
		data.put("page", list.getPageNum());
		data.put("limit", list.getPageSize());
		data.put("pages", list.getPages());
		return ResponseUtil.ok(data);
	}

//	public List<Map> getAuth(String searchContent, Integer userId) {
//		List<String> herbQuerys = Lists.newArrayList();
//		herbQuerys.add(" u.id = " + userId + " ");
//		herbQuerys.add(" h.deleted = 0 ");
//		herbQuerys.add(" uha.deleted = 0 ");
//		herbQuerys.add(" CONCAT(IFNULL(h.herb_name,''),IFNULL(h.code,''),IFNULL(h.specifications,'')) LIKE '%"
//				+ searchContent + "%' ");
//		String herbQuery = StringUtils.collectionToDelimitedString(herbQuerys, "and");
//		List<Map> maps = qualificationsService.getUserHerbs(herbQuery);
//		if (CollectionUtils.isNotEmpty(maps)) {
//			return ConvertUtils.sourceToCamelCaseMap(maps);
//		}
//		return Lists.newArrayList();
//	}

	/**
	 * 获取所有药品品类
	 * 
	 * @return
	 */
//	public Map<String, Object> getAuthHerbVoList(String searchContent, Integer page, Integer limit, Integer userId) {
//		PageHelper.startPage(page, limit);
//
//		List<String> herbQuerys = Lists.newArrayList();
//		herbQuerys.add(" u.id = " + userId + " ");
//		herbQuerys.add(" IFNULL(h.generic_name,'') !='' ");
//		herbQuerys.add(" h.deleted = 0 ");
//		herbQuerys.add(" uha.deleted = 0 ");
////		herbQuerys.add(" CONCAT(IFNULL(h.herb_name,''),IFNULL(h.code,''),IFNULL(h.specifications,'')) LIKE '%"
////				+ searchContent + "%' ");
//		herbQuerys.add(" CONCAT(IFNULL(h.generic_name,''),IFNULL(h.describe,'')) LIKE '%" + searchContent + "%' ");
//		String herbQuery = StringUtils.collectionToDelimitedString(herbQuerys, "and");
//		List<Map> herbList = qualificationsService.getUserHerbGenericDescribeNames(herbQuery);
//
////		List<Map<String, Object>> herbVoList = new ArrayList<>(herbList.size());
//
////		Map<String, Object> vo = null;
////		String id = null;
////		String code = null;
////		String herbName = null;
////		String specifications = null;
////		String measurementUnit = null;
////		for (Map map : herbList) {
////			id = MapUtils.getString(map, "herb_id");
////			code = MapUtils.getString(map, "code");
////			herbName = MapUtils.getString(map, "herb_name");
////			specifications = MapUtils.getString(map, "specifications");
////			measurementUnit = MapUtils.getString(map, "measurement_unit");
////
////			vo = new HashMap<>();
////			vo.put("id", id);
////			vo.put("code", code);
////			vo.put("name", herbName);
////			vo.put("specifications", specifications);
////			vo.put("unit", measurementUnit);
////			vo.put("content", MessageFormat.format("{0} - {1}", herbName, formatStr(specifications)));
////			vo.put("contentAll", MessageFormat.format("{0} - {1}/{2}", herbName, formatStr(specifications),
////					formatStr(measurementUnit)));
////
////			herbVoList.add(vo);
////		}
//		Page<Map<String, Object>> list = (Page) herbList;
//		if (CollectionUtils.isNotEmpty(herbList)) {
//			herbList = ConvertUtils.sourceToCamelCaseMap(herbList);
//		}
//
//		Map<String, Object> data = new HashMap();
//		data.put("list", herbList);
//		data.put("total", list.getTotal());
//		data.put("page", list.getPageNum());
//		data.put("limit", list.getPageSize());
//		data.put("pages", list.getPages());
//
//		return data;
//	}

	private String formatStr(String value) {
		return StringUtils.isEmpty(value) ? "" : value;
	}
}